﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using ProgramDioptase.CatalogItems;

namespace ProgramDioptase
{
    /// <summary>
    /// Interaction logic for MoviesLibrary.xaml
    /// </summary>
    public partial class MoviesLibrary : Window
    {
        public MoviesLibrary()
        {
            this.InitializeComponent();

            ElementRenderer.VisualizeAllElementsPanel<Movie>(this.AllMoviesPanel, DataManager.Instance.Movies, DataManager.MoviePath);
            // ElementRenderer.VisualizeAllElementsPanel(this.AllMoviesPanel, DataManager.Movies, DataManager.MoviePath);
            // ElementRenderer.AddGenresToPanel(typeof(Enumerations.MovieGenre), this.GenrePanel);
        }

        #region [WINDOW events - Close / Minimize / Moving / Closing / VisibleChanged]

        private void MoveTheWindow(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();            
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Closing -= this.Window_Closing;
            e.Cancel = true;

            DoubleAnimation animation = new DoubleAnimation()
            {
                From = (this.IsVisible) ? 1 : 0,
                To = (this.IsVisible) ? 0 : 1,
                Duration = TimeSpan.FromSeconds(0.5)
            };

            this.BeginAnimation(Window.OpacityProperty, animation);

            animation.Completed += (s, _) => this.Close();
            this.BeginAnimation(UIElement.OpacityProperty, animation);
        }

        private void CloseButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SystemCommands.CloseWindow(this);
        }

        private void MinimazeButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SystemCommands.MinimizeWindow(this);
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = (this.IsVisible) ? 0 : 1,
                To = (this.IsVisible) ? 1 : 0,
                Duration = TimeSpan.FromSeconds(0.5)
            };

            this.BeginAnimation(Window.OpacityProperty, animation);
        }

        private void ToMainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Hide();
            this.Close();

            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
        }

        #endregion

        #region [Set/Unset color of Labels and Boxes - using OnHover and OnLeave Events]

        static readonly Brush lineOnHoverColor = Brushes.GhostWhite;
        static readonly Brush lineOnLeaveColor = Brushes.CornflowerBlue;

        private void ToMainWindow_MouseMove(object sender, MouseEventArgs e)
        {
            this.TopLeftRectangle.Fill = lineOnLeaveColor;
        }

        private void ToMainWindow_MouseLeave(object sender, MouseEventArgs e)
        {
            this.TopLeftRectangle.Fill = this.Background;
        }

        #endregion

        #region [Search box & Button Events]
        
        private void OnSearchBoxTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ElementRenderer.VizualizeNewSearchResultByName<Movie>(
                this.AllMoviesPanel, this.SearchByNameBox, DataManager.Instance.Movies, DataManager.MoviePath);
        }

        private void OnSearchBoxGotFocus(object sender, RoutedEventArgs e)
        {
            ElementRenderer.SearchBoxRemoveContent(this.SearchByNameBox);
        }
     
        private void OnSearchBoxLostFocus(object sender, RoutedEventArgs e)
        {
            ElementRenderer.SearchBoxPutContent(this.SearchByNameBox);
        }
        
        #endregion

        private void SearchByGenreBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.SearchByGenreBox.Text = string.Empty;
        }

        private void SearchByGenreBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.SearchByGenreBox.Text))
            {
                this.SearchByGenreBox.Text = "Search by genre";
            }
        }

        private void SearchByGenreBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ElementRenderer.VizualizeNewSearchResultByGenre<Movie>(
                this.AllMoviesPanel, this.SearchByGenreBox, DataManager.Instance.Movies, DataManager.MoviePath);
        }

        private void SearchByYearBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ElementRenderer.VizualizeNewSearchResultByYear<Movie>(
                this.AllMoviesPanel, this.SearchByYearBox, DataManager.Instance.Movies, DataManager.MoviePath);
        }

        private void SearchByYearBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.SearchByYearBox.Text = string.Empty;
        }

        private void SearchByYearBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.SearchByYearBox.Text))
            {
                this.SearchByYearBox.Text = "Search by year";
            }
        }
    }
}